package com.bw.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.common.entity.TabEntity
import com.bw.home.databinding.CateItemBinding
import com.bw.image.GlideImageLoader

class HotServiceAdapter(private val data:MutableList<TabEntity>):RecyclerView.Adapter<HotServiceAdapter.HotItemViewHolder>() {


    // 定义点击监听接口
    interface OnItemClickListener {
        fun onItemClick(entity: TabEntity, position: Int)
    }

    // 添加监听器变量
    private var itemClickListener: OnItemClickListener? = null

    // 设置监听器的方法
    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.itemClickListener = listener
    }

    inner class HotItemViewHolder(val binding:CateItemBinding):RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HotItemViewHolder {
        val itemDataBinding = CateItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return HotItemViewHolder(itemDataBinding)
    }

    override fun getItemCount(): Int  = data.size

    override fun onBindViewHolder(holder: HotItemViewHolder, position: Int) {
        val entity = data[position]
        holder.binding.entity = entity
        holder.binding.executePendingBindings()

        holder.itemView.setOnClickListener {
            itemClickListener?.onItemClick(entity, position)
        }
    }


    //多布局
//    override fun getItemViewType(position: Int): Int {
//        return data[position].index
//    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
    }

}